package lecho.lib.hellocharts.pdf;


import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;

import com.itextpdf.text.BaseColor;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.FontFactory;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.BaseFont;


import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class PrintTable {

        public static final String DEST = "test/hello1.pdf";

        public static void main(String[] args) throws IOException,
                DocumentException {
            File file = new File(DEST);
            file.getParentFile().mkdirs();
            new PrintTable().createPdf(DEST);
        }
        public void createPdf(String dest) throws IOException, DocumentException {
            Document document = new Document();
            PdfWriter.getInstance(document, new FileOutputStream(dest));
            document.open();
            PdfPTable table = new PdfPTable(8);
            for(int aw = 0; aw < 16; aw++){
                table.addCell( setChineseCell("中文测试5abd", 12));
            }
            document.add(table);
            document.close();
        }

    public static PdfPCell setChineseCell(String string,  float size)
            throws DocumentException, IOException {
        if(string != null && "".equals(string)){
            return new PdfPCell();
        }
        Font f  = FontFactory.getFont("STSong-Light",
                "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);

        if(size < 0) {
            f.setColor(BaseColor.RED);
            size = -size;
        }
        f.setSize(size);
        PdfPCell cell = new PdfPCell(new Phrase(string, f));
        cell.setHorizontalAlignment(Element.ALIGN_LEFT);
        return cell;
    }

}
